<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsCategory extends BgcmsProcess {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	public function __destruct() {
		//
	}
	
	public function process(){
		$this->Run();
	}
	
	public function create_category() {
		$cat_name 		 = null;
		$cat_parent 	 = 0;
		$cat_description = "";
		$cat_ontop		 = "0";
		$cat_onleft		 = "0";

		if(isset($_POST['cat_name']))
			$cat_name	= @$_POST['cat_name'];
		if(isset($_POST['widget_id']))
			$widget_id	= @$_POST['widget_id'];	
		else
			$widget_id = '0';
		// in language id is the key lang NOT the id	
		if(isset($_POST['language_id']))
			$language_id	= @$_POST['language_id'];	
		else
			$language_id = 'en';
		if(isset($_POST['cat_parent']) && $_POST['cat_parent']!="")
			$cat_parent 	 = $_POST['cat_parent'];
		if(isset($_POST['cat_description']))
			$cat_description = @$_POST['cat_description'];
		if((isset($_POST['cat_parent']) && $_POST['cat_parent']!="") || (isset($_POST['widget_id']) && $_POST['widget_id']!="0"))
			$cat_onleft = "1";
		else	
			$cat_ontop = "1";
		$cat_is_active = $_POST['status'];
		
		if($_FILES['banner']['name'] != null && $_FILES['banner']['name'] != "") {
			$upload = new BgcmsUpload();
			$image = $_FILES['banner']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_banner_image($_FILES['banner']['tmp_name'], "X5", $image_type);
			} 
		}
		$listcat = BgcmsCategoryBean::get_category_by_level($this->db,$cat_parent,'');
		$cat_position = count($listcat)+1;
		$cat = array (
					'cat_name' => $cat_name,
					'widget_id'=>$widget_id,
					'lang_id'=>$language_id,
					'banner_path'=>$imagename,
					'cat_parent' => $cat_parent,
					'cat_description' => $cat_description,
					'cat_position' => $cat_position,
					'cat_ontop' => $cat_ontop,
					'cat_onleft' => $cat_onleft,
					'cat_is_active' => $cat_is_active
				);	 
		$ret = BgcmsCategory::validate_category($this->db, $cat);
		if($ret == "") {				
			$id = BgcmsCategoryBean::save_category($this->db, $cat);
			$cat['new_name'] = "";
			
			if($id > 0) {
				$message = "Category has been added";
				setcookie("message", $message,time()+5);
				if($cat_parent != 0)
					header("Location: ./?module=BgcmsContentManager&sub1=category&subcat=load_sub_category&catid=".$_POST['cat_parent']);
				else
					header("Location: ./?module=BgcmsContentManager&sub1=category");
			} else {
				$cat['message'] = _('Could not insert new category to database.');
			}
			
		} else {
			setcookie("message", $ret,time()+5);
			if($cat_parent != 0)
				header("Location: ./?module=BgcmsContentManager&sub1=category&subcat=load_sub_category&catid=".$_POST['cat_parent']."&act=newcategory") ;
				else
					header("Location: ./?module=BgcmsContentManager&sub1=category&act=newcategory") ;
		}
		$cat['cat_is_active'] = (cat_is_active == "1"?"x":"");
		$cat['cat_ontop'] = ($cat_ontop == "1"?"x":"");
		$cat['cat_onleft'] = ($cat_onleft == "1"?"x":"");
		
		
	}
	
	public function assign_page() {
		$exixts = BgcmsPageBean::get_page2cat_by_catid($this->db,$_POST['cat_id']);
		if($exixts!="")
			BgcmsPageBean::update_assign_page($this->db, $_POST['page_id'],$_POST['cat_id']);	
		else	
			BgcmsPageBean::save_assign_page($this->db, $_POST['page_id'],$_POST['cat_id']);
		if($_POST['catid']!="")
			header("Location: ?module=BgcmsContentManager&sub1=category&subcat=load_sub_category&catid=".$_POST['catid']);
		else
			header("Location: ?module=BgcmsContentManager&sub1=category");
	}
	
	public function assign_product_group() {
		$allproductgroup =	BWebshopProductGroupBean::get_all_product_group_to_assign($this->db,$_POST['cat_id']);
		if($allproductgroup!=""){
			foreach($allproductgroup as $item){
				if($_POST['sum_'.$item->product_group_id] !=""){
					$product =	BWebshopProductBean::getProductByGroupIdAssign($this->db,$item->product_group_id,$_POST['cat_id']);
					if($product!=""){
						//var_dump($product);
						foreach($product as $itempro){	
							if(isset($_POST['p_'.$itempro->product_id]) && $_POST['p_'.$itempro->product_id]!="" && $itempro->active_product==""){
								BWebshopProductGroupBean::save_assign_product($this->db, $itempro->product_id,$_POST['cat_id'],$item->product_group_id);
								
							}else if(!isset($_POST['p_'.$itempro->product_id]) && $_POST['p_'.$itempro->product_id]=="" && $itempro->active_product!=""){
								BWebshopProductGroupBean::delete_assign_product($this->db, $itempro->product_id,$_POST['cat_id'],$item->product_group_id);
							}	
						}
					}	
				}else{
					if(isset($_POST['group_'.$item->product_group_id])){
						if($_POST['exists_'.$item->product_group_id]==""){
							$product =	BWebshopProductBean::getProductByGroupIdAssign($this->db,$item->product_group_id,$_POST['cat_id']);
							if($product!=""){
								foreach($product as $itempro){	
										BWebshopProductGroupBean::save_assign_product($this->db, $itempro->product_id,$_POST['cat_id'],$item->product_group_id);
								}
							}	
						}		
					}else{
						if($_POST['exists_'.$item->product_group_id]!=""){
							BWebshopProductGroupBean::delete_assign_product($this->db,'',$_POST['cat_id'],$item->product_group_id);	
						}	
					}
				}
			}
		}
		if($_POST['catparent']!="")
			$link = '&subcat=load_sub_category&catid='.$_POST['catparent'];
		header("Location: ?module=BgcmsContentManager".$link);	
	}
	
	public function edit_category() {
		$cat_id			 = -1;
		$cat_name 		 = NULL;
		$cat_parent 	 = "0";
		$cat_description = "";
		$cat_ontop		 = "0";
		$cat_onleft		 = "0";

		if(isset($_POST['cat_id']))
			$cat_id 		 = @$_POST['cat_id'];
		if(isset($_POST['cat_name']))
			$cat_name 		 = @$_POST['cat_name'];
		if(isset($_POST['widget_id']))
			$widget_id	= @$_POST['widget_id'];	
		else
			$widget_id = '0';
		// in language id is the key lang NOT the id	
		if(isset($_POST['language_id']))
			$language_id	= @$_POST['language_id'];	
		else
			$language_id = 'en';		
		if(isset($_POST['cat_parent']) && $_POST['cat_parent']!="")
			$cat_parent 	 = @$_POST['cat_parent'];
		if(isset($_POST['cat_description']))
			$cat_description = @$_POST['cat_description'];
		if((isset($_POST['cat_parent']) && $_POST['cat_parent']!="") || (isset($_POST['widget_id']) && $_POST['widget_id']!="0"))
			$cat_onleft = "1";
		else	
			$cat_ontop = "1";
		$cat_is_active = $_POST['status'];
		$catinfo = BgcmsCategoryBean::get_category_by_id($this->db,$cat_id);
		if($_FILES['banner']['name'] != null && $_FILES['banner']['name'] != "") {
			$upload = new BgcmsUpload();
			$image = $_FILES['banner']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_banner_image($_FILES['banner']['tmp_name'], "X5", $image_type);
				@unlink("../".UPLOAD.$catinfo->banner_path);
			} else {
				$_SESSION['file_error'] = $this->l['error']['imageError'];
			}
		}
		else
			$imagename = $catinfo->banner_path;	
		$cat = array (
					'cat_id' => $cat_id,
					'cat_name' => $cat_name,
					'widget_id'=>$widget_id,
					'lang_id'=>$language_id,
					'banner_path' => $imagename,
					'cat_parent' => $cat_parent,
					'cat_description' => $cat_description,
					'cat_ontop' => $cat_ontop,
					'cat_onleft' => $cat_onleft,
					'cat_is_active' => $cat_is_active
				);	 
		$msg = BgcmsCategory::validate_category($this->db, $cat);
		 
		if($msg == "") {
			$res = BgcmsCategoryBean::update_category($this->db, $cat);
			$message = "Category has been updated";
			setcookie("message", $message,time()+5);
			/*check edit category top or left*/
			if($_POST['cat_parent']!="")
				header("Location: ./?module=BgcmsContentManager&sub1=category&subcat=load_sub_category&catid=".$_POST['cat_parent']);
			else
				header("Location: ./?module=BgcmsContentManager&sub1=category");
			/**/	
		} else {
			setcookie("message", $msg,time()+5);
			header("Location: ./?module=BgcmsContentManager&sub1=category&act=editcategory&id=".$cat_id) ;
		}
	}
	
	public function delete_category() {
		$ID = $_POST['id'];
		$old_pos = $_POST['cat_position'];
			if(isset($_POST['catparent']) && $_POST['catparent']!="")
				$id = $_POST['catparent'];
			else
				$id = 0;
			$listcat = BgcmsCategoryBean::get_category_by_level($this->db,$id,$ID);	
			
			if($listcat!="") {
				foreach($listcat as $item) {
					if($item->cat_position>$old_pos) {
						$new_position = $item->cat_position -1;
						BgcmsCategoryBean::update_position_category($this->db,$item->cat_id,$new_position);
					}
				}	
			}
		$path = BgcmsCategoryBean::get_category_by_id($this->db,$ID);	
		@unlink("../".UPLOAD.$path->banner_path);	
		$exixts = BgcmsPageBean::get_page2cat_by_catid($this->db,$ID);
		if($exixts!=""){
			BgcmsPageBean::delete_page($this->db, $exixts->page_id);	
		}	
		$res = BgcmsCategoryBean::delete_category($this->db, $ID);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res) {
			$arr['message'] = _("Category list has been updated!");
			setcookie("message", $message,time()+5);
			$arr['code'] = 0;
		} else {
			$arr['message'] = _("Could not delete category, id: ") . $ID;
			unset($_SESSION['message']);
		}
		
		echo array2json($arr);
	}
	
	public function setactive_category() {
		$id = $_POST['id'];
		$value = $_POST['value'];
		$res = BgcmsCategoryBean::update_active_category($this->db, $id,$value);
	}
	
	public function setdefault_category() {
		$id = $_POST['id'];
		$value = $_POST['value'];
		$default = BgcmsCategoryBean::get_default_category($this->db);
		if($default!="")
			BgcmsCategoryBean::update_default_category($this->db, $default->cat_id,'0');
		$res = BgcmsCategoryBean::update_default_category($this->db, $id,$value);
	}
	
	public function change_position() {
		$old_pos = $_POST['position'];
		$cat_id = $_POST['cat_id'];
		if(isset($_POST['cat_parent']) && $_POST['cat_parent']!="")
			$id = $_POST['cat_parent'];
		else
			$id = 0;
		$listcat = BgcmsCategoryBean::get_category_by_level($this->db,$id,$_POST['cat_id']);
		if($_POST['type_change'] == 'first') {
			foreach($listcat as $item) {
				if($item->cat_position<$old_pos) {
					$new_position = $item->cat_position +1;
					BgcmsCategoryBean::update_position_category($this->db,$item->cat_id,$new_position);
				}
			}	
			BgcmsCategoryBean::update_position_category($this->db,$cat_id,'1');	
		}
		else if($_POST['type_change'] == 'last') {
			foreach($listcat as $item) {
				if($item->cat_position>$old_pos) {
					$new_position = $item->cat_position-1;
					BgcmsCategoryBean::update_position_category($this->db,$item->cat_id,$new_position);
				}
			}
			$newpos = count($listcat)+1;
			BgcmsCategoryBean::update_position_category($this->db,$cat_id,$newpos);				
		}
		else if($_POST['type_change'] == 'after') {
			$after_id = $_POST['position_id'];
			foreach($listcat as $item) {
				if($old_pos>$after_id) {
					if($item->cat_position>$after_id && $item->cat_position<$old_pos) {
						$new_position = $item->cat_position+1;
						BgcmsCategoryBean::update_position_category($this->db,$item->cat_id,$new_position);	
					}
				} else {
					if($item->cat_position<=$after_id && $item->cat_position>$old_pos) {
						$new_position = $item->cat_position-1;
						BgcmsCategoryBean::update_position_category($this->db,$item->cat_id,$new_position);	
					}
				}	
			}	
			if($old_pos>$after_id)
				BgcmsCategoryBean::update_position_category($this->db,$cat_id,$after_id+1);
			else
				BgcmsCategoryBean::update_position_category($this->db,$cat_id,$after_id);
	
		}
		//Back URL after change position
		if($_POST['cat_parent']!="")
			header("Location: ./?module=BgcmsContentManager&sub1=category&subcat=load_sub_category&catid=".$_POST['cat_parent']);
		else
			header("Location: ./?module=BgcmsContentManager&sub1=category");
	}
	
	public function validate_category($db, $cat) {
		$msg = "";		
		if(!isset($_POST['cat_name']) || $_POST['cat_name'] == "") {
			$msg = _("Category name could not be empty");
		}
		if($cat['cat_parent']!='0' && $cat['cat_ontop'] == "1"){
				$msg .= ($msg==""?"":"<br />") . 
						 _("You cannot set a category on top menu while it's parent is on top menu.");
		}
		
		if($cat['cat_ontop'] == "1"){
			$alltop = BgcmsCategoryBean::count_top_category($db);	
			if($alltop >= MAX_CATEGORY_TOP_LEVEL )
				$msg .= ($msg==""?"":"<br />") . 
						_("Number of nested top category is exceeded. Maximum is ") . " " .
						MAX_CATEGORY_TOP_LEVEL;
		}
		
		//$parentCat = BgcmsCategoryBean::get_category_by_id($db, $cat['cat_parent']);
		
		/*if($parentCat != null && $parentCat->cat_onleft == "1") {
			if($cat['cat_ontop'] == "1") {
				$msg .= ($msg==""?"":"<br />") . 
						 _("You cannot set a category on top menu while it's parent is on left menu.");
			}
		}*/
		
		/*if(isset($_POST['cat_parent'])) {
			$lv = BgcmsCategoryBean::get_level($db, $_POST['cat_parent']);
			if($lv > (MAX_CATEGORY_LEVEL-1)) {
				$msg .= ($msg==""?"":"<br />") . 
						_("Number of nested category is exceeded. Maximum is ") . " " .
						MAX_CATEGORY_LEVEL;
			}
		}*/
		
		return $msg;
	}
}
?>